package com.ruoyi.system.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    //static Calendar cal = Calendar.getInstance();
    //获取当前时间
    public static Date Now()
    {
        return ParseDateTime(Format(new Date(),"yyyy-MM-dd HH:mm:ss"));
    }
    //格式化时间
    public static String Format(Date date, String formatStr){
        SimpleDateFormat simpleDate = new SimpleDateFormat(formatStr);
        return simpleDate.format(date);
    }

    //添加分钟
    public static Date AddMinutes(Date date, int minutes){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minutes);
        return cal.getTime();
    }
    //添加天数
    public static Date AddDays(Date date, int days){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }
    //添加月数
    public static Date AddMonth(Date date, int months){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, months);
        return cal.getTime();
    }
    //添加年数
    public static Date AddYear(Date date, int years){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR, years);
        return cal.getTime();
    }
    //字符串转日期
    public static Date ParseDate(String date){
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDate.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }
    //字符串转日期时间
    public static Date ParseDateTime(String date){
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDate.parse(date);
        } catch (ParseException e) {
           return null;
        }
    }
    //指定格式字符串转日期时间
    public static Date ParseDateTime(String date, String pattern){
        SimpleDateFormat simpleDate = new SimpleDateFormat(pattern);
        try {
            return simpleDate.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    //获取当前时间的小时部分
    public static Integer GetHour(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.HOUR_OF_DAY);
    }

    //获取间隔天数
    public static Integer GetDiffDays(Date startDate, Date endDate) {
        long diffTime=endDate.getTime()-startDate.getTime();
        int days = (int)(diffTime/1000/60/60/24);
        return days;
    }
    //获取间隔分钟
    public static Integer GetDiffMinutes(Date startDate, Date endDate) {
        long diffTime=endDate.getTime()-startDate.getTime();
        int minutes = (int)(diffTime/1000/60);
        return minutes;
    }

    //获取星期几
    public static String GetWeekday(Date date) {
        String[] weeks = {"周日","周一","周二","周三","周四","周五","周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        return weeks[week_index];
    }

    /*根据出生日期算出年龄*/
    public static int getAge(String birthday) {
        int returnAge=0;
        String strBirthdayArr = birthday.replace("/","-");
        Date date=ParseDate(strBirthdayArr);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int birthYear = cal.get(Calendar.YEAR);
        int birthMonth = cal.get(Calendar.MONTH) + 1;
        int birthDay = cal.get(Calendar.DATE);

        Date d = new Date();
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(d);
        int nowYear =cal2.get(Calendar.YEAR);
        int nowMonth = cal2.get(Calendar.MONTH) + 1;
        int nowDay = cal2.get(Calendar.DATE);

        if (nowYear == birthYear) {
            returnAge = 0; //同年 则为0岁
        } else {
            int ageDiff = nowYear - birthYear; //年之差
            if (ageDiff > 0) {
                if (nowMonth == birthMonth) {
                    int dayDiff = nowDay - birthDay; //日之差
                    if (dayDiff < 0) {
                        returnAge = ageDiff - 1;
                    } else {
                        returnAge = ageDiff;
                    }
                } else {
                    int monthDiff = nowMonth - birthMonth; //月之差
                    if (monthDiff < 0) {
                        returnAge = ageDiff - 1;
                    } else {
                        returnAge = ageDiff;
                    }
                }
            } else {
                returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
            }
        }
        return returnAge; //返回周岁年龄
    }
}
